package com.nustalk;

import org.jivesoftware.smack.XMPPException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private EditText username, password;
	private Button loginButton;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		username = (EditText)findViewById(R.id.username);
		password = (EditText)findViewById(R.id.password);
		loginButton = (Button) findViewById(R.id.loginButton);
		loginButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), username.getText().toString(), Toast.LENGTH_SHORT).show();
				Toast.makeText(getApplicationContext(), password.getText().toString(), Toast.LENGTH_SHORT).show();
				//Boolean valid = NUSTalkManager.getManager().validate(username.getText().toString(), password.getText().toString());
				//System.out.println("Valid " + valid);
				//Toast.makeText(getApplicationContext(), valid ? "OK": "NO", Toast.LENGTH_SHORT).show();
				try {
					NUSTalkManager.getManager().login(username.getText().toString(), password.getText().toString());
					Intent intentMain = new Intent(getApplicationContext(), MainActivity.class);
					startActivityForResult(intentMain, 0);
				} catch (InvalidLoginException e) {
					Toast.makeText(getApplicationContext(), "Invalid login", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				} catch (XMPPException e) {
					Toast.makeText(getApplicationContext(), "Connection error", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				
			}
		});
	}
}
